<?php
/**
 * Created by PhpStorm.
 * User: jiangjunxian
 * Date: 2016/12/2
 * Time: 16:10
 */

namespace app\modules\admin\components;


use app\modules\admin\models\AuthItem;
use app\modules\admin\models\search\AuthItemSearch;
use Yii;
use yii\filters\VerbFilter;
use yii\rbac\Item;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;

/**
 * @property integer $type
 * Class ItemController
 * @package app\modules\admin\components
 */
abstract class ItemController extends Controller
{
	public function behaviors()
	{
		return [
			'verbs' => [ // 行为限制
				'class' => VerbFilter::className(),
				'actions' => [
					'delete' => ['post'],
					'assign' => ['post'],
					'remove' => ['post'],
				],
			],
		];
	}

	/**
	 * 列表
	 * @return string
	 */
	public function actionIndex() {
		$searchModel = new AuthItemSearch(['type'=>$this->type]);
		$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
		return $this->render('index',[
			'dataProvider' => $dataProvider,
			'searchModel' => $searchModel
		]);
	}


	/**
	 * 新建
	 * @return string|Response
	 */
	public function actionCreate() {
		$model = new AuthItem();
		$model->type = $this->type;
		if($model->load(Yii::$app->request->post()) && $model->save()) {
			return $this->redirect(['view', 'id' => $model->name]);
		}
		return $this->render('create', ['model'=>$model]);
	}

	/**
	 * 查看
	 * @param $id
	 * @return string
	 */
	public function actionView($id) {
		$model = $this->findModel($id);
		return $this->render('view', [ 'model' => $model ]);
	}

	/**
	 * 编辑
	 * @param $id
	 * @return string|Response
	 */
	public function actionUpdate($id) {
		$model = $this->findModel($id);
		if($model->load(Yii::$app->request->post()) && $model->save()) {
			return $this->redirect(['view', 'id'=> $model->name]);
		}
		return $this->render('update', ['model'=>$model]);
	}

	/**
	 * @param $id
	 * @return Response
	 */
	public function actionDelete($id) {
		$model = $this->findModel($id);
		Yii::$app->authManager->remove($model->item);
		return $this->redirect(['index']);
	}

	/**
	 * 获取权限
	 * @param $id
	 * @return array
	 */
	public function actionAssign($id) {
		$items = Yii::$app->request->post('items', []);
		Yii::trace($items);
		$model = $this->findModel($id);
		$success = $model->addChildren($items);
		Yii::$app->response->format = Response::FORMAT_JSON;

		return array_merge($model->getItems(), ['success' => $success]);
	}

	/**
	 * 删除权限
	 * @param $id
	 * @return array
	 */
	public function actionRemove($id) {
		$items = Yii::$app->request->post('items', []);
		$model = $this->findModel($id);
		$success = $model->removeChildren($items);
		Yii::$app->response->format = Response::FORMAT_JSON;
		return array_merge($model->getItems(), ['success' => $success]);
	}

	/**
	 * @return string
	 */
	public function getViewPath() {
		return $this->module->getViewPath() . DIRECTORY_SEPARATOR . 'item';
	}

	/**
	 * @return integer
	 */
	abstract public function getType();

	/**
	 * @return array
	 */
	abstract public function labels();

	/**
	 * 通过权限的名称获取AuthItem对象
	 * @param string $id
	 * @return AuthItem
	 * @throws NotFoundHttpException
	 */
	protected function findModel($id) {
		$mgr = Yii::$app->authManager;
		$item = $this->type === Item::TYPE_ROLE ? $mgr->getRole($id) : $mgr->getPermission($id);
		if($item !== null) {
			return new AuthItem($item);
		} else {
			throw new NotFoundHttpException('请求的页面不存在。');
		}
	}
}